package de.slothsoft.commons.swing;

import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JToolBar;


public class JToolBarManager {

    private final JToolBar toolBar;
    private ActionListener defaultActionListener;

    public JToolBarManager() {
	this(new JToolBar());
    }

    public JToolBarManager(JToolBar toolBar) {
	this.toolBar = toolBar;
    }

    public JToolBar getToolBar() {
	return this.toolBar;
    }

    public JButton add(Icon icon, ActionListener listener) {
	return add(icon, null, listener);
    }

    public JButton add(Icon icon, String tooltip, ActionListener listener) {
	JButton button = SwingUtil.createToolBarButton(icon, tooltip);
	if (this.defaultActionListener != null) {
	    button.addActionListener(this.defaultActionListener);
	}
	button.addActionListener(listener);
	this.toolBar.add(button);
	return button;
    }

    public void addSeparator() {
	this.toolBar.addSeparator();
    }

}
